package heven.holt.library.base

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding

abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
    private var _binding: VB? = null

    protected val binding: VB
        get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = bindViewBinding()
        val decorationView = createDecorationView()
        setContentView(decorationView ?: binding.root)

        initView()
        initData()
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }

    /**
     * 某些情况下，ViewBinding的视图并不是根布局
     */
    open fun createDecorationView(): View? = null

    abstract fun bindViewBinding(): VB

    abstract fun initView()

    abstract fun initData()
}